<?php
namespace app\controller;

use app\BaseController;
// use app\controller\ForwardController;
use app\model\TowerAnticollision;
use app\model\TowerCrane;
use app\model\TowerCraneNdata;
// use think\facade\Log;
use think\facade\Db;
use think\Request;

/* 塔机设备控制器 */

class ExternalController extends BaseController
{
    //嵌入页面防碰撞接口
    public function anticollisionEmbed(Request $request)
    {
        if ($request->has('pid')) {
            $pid = $request->param('pid');
            $list = TowerCrane::where('pid', $pid)->field('eid as id,ename as name')->select();
            if (count($list) > 0) {
                $eids = [];
                $status_data = [];
                $Anticollision_data = [];
                $list_data = [];
                foreach ($list as &$option0) {
                    $eids[] = $option0->id;
                    $option0->online_status = 0;
                    $option0->height = '--';
                    $option0->wind_speed = '--';
                    $option0->status = [
                        'weight' => '0',
                        'height' => '0',
                        'amplitude' => '0',
                        'round' => '0',
                        'moment_percent' => '0',
                        'wind_speed' => '0',
                        'angle_x' => '0',
                    ];
                    $option0->Anticollision = [
                        'main_name' => $option0->name,
                        'main_arm_length' => $option0->arm_length,
                        'round' => 0,
                        'slave_names' => [],
                        'slave_arm_lengths' => [],
                        'slave_distances' => [],
                        'slave_angles' => [],
                        'angles' => [],
                    ];
                    $list_data[$option0->id] = $option0;
                }
                unset($option0);
                $status = TowerCraneNdata::whereIn('eid', $eids)->field('eid,weight,height,amplitude,round,moment_percent,wind_speed,angle_x,update_time')->select();
                $TowerAnticollision = TowerAnticollision::whereIn('main_id', $eids)->field('main_id,main_name,main_arm_length,slave_ids,slave_names,slave_arm_lengths,slave_distances,slave_angles')->select();
                foreach ($status as $statu) {
                    $status_data[$statu->eid] = $statu;
                }
                foreach ($TowerAnticollision as $Anticollision) {
                    $Anticollision_data[$Anticollision->main_id] = $Anticollision;
                }
                foreach ($list as &$option) {
                    if (array_key_exists($option->id, $status_data)) {
                        $option->height = $status_data[$option->id]->height;
                        $option->wind_speed = $status_data[$option->id]->wind_speed;
                        $option->status = [
                            'weight' => $status_data[$option->id]->weight,
                            'height' => $status_data[$option->id]->height,
                            'amplitude' => $status_data[$option->id]->amplitude,
                            'round' => $status_data[$option->id]->round,
                            'moment_percent' => $status_data[$option->id]->moment_percent,
                            'wind_speed' => $status_data[$option->id]->wind_speed,
                            'angle_x' => $status_data[$option->id]->angle_x,
                        ];
                    }
                    if (array_key_exists($option->id, $Anticollision_data)) {
                        $Anticollision = [
                            'main_name' => $option->name,
                            'main_arm_length' => $Anticollision_data[$option->id]->main_arm_length,
                            'round' => array_key_exists($option->id, $status_data) ? $status_data[$option->id]->round : 0,
                        ];
                        $slave_ids = explode(',', $Anticollision_data[$option->id]->slave_ids);
                        $slave_names = [];
                        foreach ($slave_ids as $slave_id) {
                            $slave_names[] = array_key_exists($slave_id, $list_data) ? $list_data[$slave_id]->name : '';
                        }
                        $Anticollision['slave_names'] = $slave_names;
                        $Anticollision['slave_arm_lengths'] = explode(',', $Anticollision_data[$option->id]->slave_arm_lengths);
                        $Anticollision['slave_distances'] = explode(',', $Anticollision_data[$option->id]->slave_distances);
                        $Anticollision['slave_angles'] = explode(',', $Anticollision_data[$option->id]->slave_angles);
                        $round = [];
                        foreach ($slave_ids as $eid) {
                            if (array_key_exists($eid, $status_data)) {
                                $round[] = $status_data[$eid]->round;
                            } else {
                                $round[] = 0;
                            }
                        }
                        $Anticollision['angles'] = $round;
                        $option->Anticollision = $Anticollision;
                    }
                };
                unset($option);
                return success($list);
            } else {
                return fail(null, "暂无数据");
            }
        } else {
            return fail(null, "参数有误");
        }

    }
    public function iccidSelect(Request $request)
    {
        $iccid = $request->param('iccid');
        $re = DB::table('simcard')->where('iccid', $iccid)->find();
        return success($re);
    }
    public function hejson(Request $request)
    {
        //何少之--苏州
        $re = [
            "msg" => "操作成功",
            "code" => 0,
            "data" => [
                [
                    "dev_id" => " J56212924_1",
                    "dev_type" => 1,
                    "dev_sn" => "J56212924_1",
                    "dev_url" => "https://open.ys7.com/v3/openlive/J56212924_1_1.m3u8?expire=1731397907&id=646014703226388480&t=c783bb097fcd95f27b56164f1db3bb3ae9eb52906e1cf278cf0f5b7763c1197f&ev=100",
                    "is_online" => 1,
                    "dev_name" => "实名制",
                ],
                [
                    "dev_id" => " J56212924_2",
                    "dev_type" => 2,
                    "dev_sn" => "J56212924_2",
                    "dev_url" => "https://open.ys7.com/v3/openlive/J56212924_2_1.m3u8?expire=1731398082&id=646015438521630720&t=a251492136cbc9f084f73efdb105dea9e365924abb2371db2e0e7838ba572102&ev=100",
                    "is_online" => 1,
                    "dev_name" => "大门",
                ],
                [
                    "dev_id" => " J56212924_3",
                    "dev_type" => 3,
                    "dev_sn" => "J56212924_3",
                    // "dev_url"=> "https://open.ys7.com/v3/openlive/J56212924_4_1.m3u8?expire=1731398190&id=646015888620785664&t=00518e63ba66963de615cb192479125ebdbe96e55373d907b2c0820139d68a86&ev=100",
                    "dev_url" => "https://open.ys7.com/v3/openlive/J56212924_3_1.m3u8?expire=1731550906&id=646656427254677504&t=b24a45beddbb493d5b8ba6a45a373a55b555afc93f86ee0a2aadc52b7bc5912b&ev=100",
                    "is_online" => 1,
                    "dev_name" => "施工现场",
                ],
            ],
        ];
        return json($re);
    }
    public function hejson20240321(Request $request)
    {
        $re = [
            "msg" => "操作成功",
            "code" => 0,
            "data" => [
                [
                    "dev_id" => "G82415006_1",
                    "dev_type" => 1,
                    "dev_sn" => "G82415006_1",
                    "dev_url" => "https://open.ys7.com/v3/openlive/G82415006_1_1.m3u8?expire=1742049322&id=690689975234068480&t=e9dbd1b0a62e1ca978a2cfb556e1641b04b9b26e2c9e8e7bccf619dd00e9e1d3&ev=100",
                    "is_online" => 1,
                    "dev_name" => "施工现场",
                ],
                [
                    "dev_id" => " G82415006_2",
                    "dev_type" => 2,
                    "dev_sn" => "G82415006_2",
                    "dev_url" => "https://open.ys7.com/v3/openlive/G82415006_2_1.m3u8?expire=1742049537&id=690690879060455424&t=869f63c278f5a26bfe9e50c9a65e356b36d356e3ad82f8a47e07d1da2b5c0d40&ev=100",
                    "is_online" => 1,
                    "dev_name" => "工地大门",
                ],
                [
                    "dev_id" => " G82415006_3",
                    "dev_type" => 3,
                    "dev_sn" => "G82415006_3",
                    "dev_url" => "https://open.ys7.com/v3/openlive/G82415006_3_1.m3u8?expire=1742049615&id=690691202088968192&t=09f22d4a0abb190f8929be0a37ae8d5bf7e2034db3e7eac2a5378a8cdad4b046&ev=100",
                    "is_online" => 1,
                    "dev_name" => "车辆冲洗平台",
                ],
            ],
        ];
        return json($re);
    }
}
